//
//  NewProductsTableViewCell.m
//  Beauty
//
//  Created by Snow on 16/8/30.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "NewProductsTableViewCell.h"
#import "NewProductCollectionViewCell.h"
#import "GXNetWorkingParser.h"
#import "ModelOfNewProducts.h"
#import "ModelOfCycleImage.h"
#import "GXMacro.h"
@interface NewProductsTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray *arrayOfNewProducts;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@end
@implementation NewProductsTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self handleData];
        self.layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 10;
        _layout.minimumInteritemSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:_layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.contentView.mas_top);
            make.left.mas_equalTo(self.contentView.mas_left);
            make.right.mas_equalTo(self.contentView.mas_right);
            make.bottom.mas_equalTo(self.contentView.mas_bottom);
        }];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[NewProductCollectionViewCell class] forCellWithReuseIdentifier:@"NewProductCollectionViewCell"];
    }
    return self;
}
- (void)handleData
{
    _arrayOfNewProducts = [NSMutableArray array];
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile/index.php?act=index&op=index_1_5" par:nil success:^(id responseObject) {
        NSDictionary *dictionary = [responseObject objectForKey:@"datas"];
        NSArray *array = dictionary[@"goods_new"];
        for (NSDictionary *dic in array) {
            ModelOfNewProducts *model = [[ModelOfNewProducts alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_arrayOfNewProducts addObject:model];
        }
        [_collectionView reloadData];
    } filed:^(NSError *error) {
        
    }];
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    self.layout.itemSize = CGSizeMake(self.contentView.bounds.size.width / 2, self.contentView.bounds.size.height);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _arrayOfNewProducts.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NewProductCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NewProductCollectionViewCell" forIndexPath:indexPath];
    ModelOfNewProducts *model = [_arrayOfNewProducts objectAtIndex:indexPath.row];
    [cell.imageOfGoods sd_setImageWithURL:[NSURL URLWithString:model.goods_image]];
    if (model.goods_name.length < 8) {
        cell.labelOfName.text = model.goods_name;
    } else {
    cell.labelOfName.text = [model.goods_name substringToIndex:8];
    }
    cell.labelOfPrice.text = [NSString stringWithFormat:@"￥ %@" , model.goods_price];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(newProductWithNSString:)]) {
        ModelOfNewProducts *model = [_arrayOfNewProducts objectAtIndex:indexPath.row];
        [self.delegate newProductWithNSString:model.goods_id];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
